package com.dzh.spring.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dzh.spring.bean.User;
import com.dzh.spring.auth.HasPermission;
import com.dzh.spring.auth.Role;
import com.dzh.spring.service.UserService;

@Controller
@Scope("prototype")
@RequestMapping("/user")
public class UserContrller {
	private ThreadLocal<Integer> seqNum = new ThreadLocal<Integer>() {
		public Integer initialValue() {
			return 0;
		}
	};

	private int i = 0;
	private static Logger log = LoggerFactory.getLogger(UserContrller.class);
	@Autowired
	private UserService UserService;

	// ②获取下一个序列值

	public int getNextNum() {
		// System.out.println(Thread.currentThread().getName());
		seqNum.set(seqNum.get() + 1);
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return seqNum.get();

	}

	/**
	 * 使用 ModelAndView 来收发参数
	 * 
	 * @param name
	 * @param password
	 * @return
	 */
	@HasPermission(Role.ADMIN)
	@RequestMapping(value = "/save", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	public ModelAndView save(String name, String password,HttpServletRequest request) {
		System.out.println("接收到的用户信息：" + request.getParameter("name"));

		ModelAndView mov = new ModelAndView();
		mov.setViewName("home");
		mov.addObject("msg", "保存成功 name :" + name);
		log.debug("保存成功");
		UserService.saveUser();
		return mov;
	}

	/**
	 * 使用 Model来收发参数
	 * 
	 * @param msg
	 * @param password
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(String msg, String password, Model model) {
		System.out.println("当前线程:" + Thread.currentThread().getName() + " 当前对象:" + this + "   getNextNum "
				+ getNextNum() + "   i = " + (++i));
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		log.debug("保存成功");
		UserService.saveUser();
		model.addAttribute("msg", msg);
		return "home";
	}

	/**
	 * 使用 @RequestParam 来修改参数映射名 user/update?name={name}
	 * 
	 * @param name
	 * @param password
	 * @return
	 */
	@RequestMapping(value = "/update")
	public ModelAndView modify(@RequestParam("name") String name, String password) {
		System.out.println("接收到的用户信息：" + name);

		ModelAndView mov = new ModelAndView();
		mov.setViewName("home");
		mov.addObject("msg", "修改成功 name :" + name);
		log.debug("修改成功");
		UserService.updateUser();
		return mov;
	}

	/**
	 * 使用Map<String,Object> 来想前端 推送数据 user/del/{userId}
	 * 
	 * @param name
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/del/{userId}")
	public String del(@PathVariable("userId") String name, Map<String, Object> model) {
		System.out.println("接收到的用户信息：" + name);
		model.put("msg", "删除" + name + "成功");
		log.debug("删除成功");
		UserService.deleteUser();
		return "home";
	}

	/**
	 * 使用常规的HttpServletRequest 来接收数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/query")
	public String query(HttpServletRequest request) {
		String name = request.getParameter("userId");
		System.out.println("接收到的用户信息：" + name);
		log.debug("删除成功");
		request.setAttribute("msg", name);
		return "home";
	}

	/**
	 * 返回 json 格式
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/findUser")
	public @ResponseBody User findUser(@RequestParam("userId") int id) {
		return UserService.findUser(id);
	}

	@RequestMapping(value = "/json/findUser")
	public ResponseEntity<User> getUser(@RequestParam("userId") int id) {
		User user = UserService.findUser(id);
		ResponseEntity<User> rsp = new ResponseEntity<User>(user, HttpStatus.OK);
		return rsp;
	}

	@RequestMapping(value = "/json/queryUsers")
	public ResponseEntity<List<User>> queryUsers(@RequestParam("userId") int id) {
		List<User> list = UserService.queryUsers(100);
		ResponseEntity<List<User>> rsp = new ResponseEntity<List<User>>(list, HttpStatus.OK);
		return rsp;
	}

}
